package com.yztc.yidoutang.fragment.zhuye_fragment;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stx.xhb.xbanner.XBanner;
import com.yztc.yidoutang.adapter.ZhuYeVPAdapter;
import com.yztc.yidoutang.bean.BannerBean;
import com.yztc.yidoutang.bean.CardBean;
import com.yztc.yidoutang.bean.TabBean;
import com.yztc.yidoutang.bean.ZhuYeBean;
import com.yztc.yidoutang.http.zhuye.ZhuYeDao;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import rx.Subscriber;

/**
 * 主页Fragment中相当于MVP中Model层的类
 */
public class ZhuYeModel implements ZhuYeContract.Model {
    private List<BannerBean> bannerBeans;
    private List<TabBean> tabBeans;
    private CardBean cardBean;
    private ZhuYeVPAdapter adapter;

    @Override
    public void initBaseData(final Context context, final XBanner banner, final ImageView card, final CircleImageView header, final TextView title,
                             final TextView author, final TabLayout tabs, final ViewPager vp) {
        ZhuYeDao.getZhuYeData(new Subscriber<ZhuYeBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(ZhuYeBean zhuYeBean) {
                // 初始化Banner中的数据
                bannerBeans = zhuYeBean.getData().getBanner();
                final List<String> imageUrls = new ArrayList<String>();
                for (BannerBean banner : bannerBeans) {
                    imageUrls.add(banner.getImage());
                }
                banner.setData(imageUrls);
                banner.setmAdapter(new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, View view, int position) {
                        Glide.with(context).load(imageUrls.get(position)).into((ImageView) view);
                    }
                });
                // 初始化CardView中的数据
                cardBean = zhuYeBean.getData().getMaster().get(0);
                Glide.with(context).load(cardBean.getPic()).into(card);
                Glide.with(context).load(cardBean.getUserPic()).into(header);
                title.setText(cardBean.getTitle());
                author.setText(cardBean.getUserName());
                // 初始化TabLayout中的数据
                tabBeans = zhuYeBean.getData().getNav();
                for (TabBean tab : tabBeans) {
                    tabs.addTab(tabs.newTab().setText(tab.getTitle()));
                }
                initViewPager(context, vp);
                tabs.setupWithViewPager(vp);
                for (int i = 0; i < tabBeans.size(); i++) {
                    tabs.getTabAt(i).setText(tabBeans.get(i).getTitle());
                }
            }
        });
    }

    @Override
    public void onChangeClicked(boolean isChecked) {
        adapter.setRecycleMode(isChecked);
    }

    private void initViewPager(Context context, ViewPager vp) {
        List<String> titles = new ArrayList<>();
        for (TabBean tab : tabBeans) {
            titles.add(tab.getTitle());
        }
        adapter = new ZhuYeVPAdapter(context, titles);
        vp.setAdapter(adapter);
    }
}